home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / examples / lexyacc / cl / fibs.cl < prev    next >
Encoding:
Text File  |  1997-08-18  |  862 b   |  28 lines  |  [TEXT/R*ch]

  1. (* Compute the infinite list of Fibonacci numbers efficiently  *)
  2.  
  3. letrec
  4.   zipwith = \f.\xs.\ys.
  5.             case xs of
  6.               <1>      -> pack{1};
  7.               <2> x xr -> case ys of
  8.                             <1>      -> pack{1};
  9.                             <2> y yr -> pack{2, f x y, zipwith f xr yr}
  10.                       end
  11.             end;
  12.   head = \xs. case xs of
  13.                 <1>      -> 0;
  14.                 <2> x xr -> x
  15.               end;
  16.   tail = \xs. case xs of
  17.                 <1>      -> pack{1};
  18.                 <2> x xr -> xr
  19.               end;
  20.   add = \x.\y.x+y;
  21.   fibs = pack{2, 1, pack{2, 1, zipwith add fibs (tail fibs)}};
  22.   take = \n.\xs.case xs of
  23.                   <1>      -> pack {1} ;
  24.                   <2> x xr -> if n=0 then pack {1}
  25.                               else pack {2, x, take (n-1) xr}
  26.                 end
  27. in fibs
  28.